package com.fluidops.datacatalog.reltables.model;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.openrdf.model.URI;

import com.fluidops.datacatalog.reltables.labelbase.ILabelSource;
import com.fluidops.datacatalog.reltables.util.LRUCache;

public class Entity {

	public static final int CACHE_SIZE = 1000;
	private static Map<URI, Entity> entityCache = new LRUCache<URI, Entity>(CACHE_SIZE);
	
	private final URI name;
	private final Collection<String> labels;
	
	public static Entity getInstance(final URI name, final Set<String> entityLabel, final ILabelSource ls){
		if(name == null || entityLabel == null){
			throw new IllegalArgumentException("Entity URI and label must not be null!");
		}
		if(!entityCache.containsKey(name)){
			entityCache.put(name, new Entity(name, ls.getLabel(entityLabel)));
		}
		return entityCache.get(name);
	}
	
	private Entity(URI name, Collection<String> label){
		this.labels = label;
		this.name = name;
	}

	public URI getName(){
		return this.name;
	}
	
	public Collection<String> getLabels(){
		return this.labels;
	}

	public boolean equals(Object o){
		if(o instanceof Entity){
			Entity e = (Entity) o;
			// labels are not considered, because they're equal if the name is identical
			return this.name.equals(e.name);
		}
		return false;
	}
	
	public int hashCode(){
		return this.name.hashCode();
	}
	
}
